package com.web.rss.gui;

import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.html.HTMLDocument;

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndEntryImpl;

public class FeedArticleScrollPane extends JScrollPane {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7503259198497750357L;
	private SyndEntryImpl displayedArticle;
	private String title;
	private String link;
	private String text;
	public JTextPane articlePane;
	
	/**
	 * 
	 * @param article
	 */
	public FeedArticleScrollPane (SyndEntryImpl article) {
		displayedArticle = article;
		articlePane = new JTextPane();
		articlePane.setEditable(false);
		this.setViewportView(articlePane);
		articlePane.setVisible(true);
		updateDisplay(article);
		
		
	}
	
	public void updateDisplay(SyndEntryImpl article) {
		displayedArticle = article;
		setTitle(displayedArticle.getTitle());
		setLink(displayedArticle.getLink());
		SyndContent contents = displayedArticle.getDescription();
		
		String fmtText = "<html><h1><a href=\"" + getLink() + "\">" + getTitle() + "</a></h1>" +
						"<p>" + contents.getValue() + "</p></html>";
		//System.out.println(fmtText);
		
		articlePane.setContentType("text/html");
		articlePane.setText(fmtText);
		articlePane.setVisible(true);
		this.setVisible(true);
		
	}
	
	public String getTitle() {
		return this.title;
	}
	
	public String getLink() {
		return this.link;
	}
	
	public String getText() {
		return this.text;
	}
	
	public void setTitle(String ttl) {
		this.title = ttl;
	}
	
	public void setLink(String url) {
		// TODO check for validity (init an URL object).
		try {
			@SuppressWarnings("unused")
			URL test = new URL(url);
		}
		catch (MalformedURLException ex) {
			
		}
		this.link = url;
	}
	
	public void setDoc(String textToSet) {
		this.text = textToSet;
	}
}
